﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using Microsoft.LightSwitch.Details;

namespace LightSwitchApplication
{
    public partial class StockManagement
    {
        partial void Warehousing_CanExecute(ref bool result)
        {
            // Write your code here.
            result = this.PurchaseOrdersDelivered != null &&
                this.PurchaseOrdersDelivered.Where(orders => orders.IsClosed == true).Any();

        }

        partial void Warehousing_Execute()
        {
            // Write your code here.
            this.Save();
            this.Refresh();
        }

        
        partial void StockManagement_Saving(ref bool handled)
        {
            // Write your code here.
            try
            {
                this.DataWorkspace.ApplicationData.SaveChanges();
                handled = true;
            }
            catch (ConcurrencyException ex)
            {
                var save = false;
                foreach (IEntityObject ec in ex.EntitiesWithConflicts)
                {
                    String a = ec.Details.Name;
                    var isClosedConflict = from p in ec.Details.EntityConflict.ConflictingProperties
                                           where p.Property.Name.ToLower() == "isclosed"
                                           select p;

                    var drugStockConflict = from p in ec.Details.EntityConflict.ConflictingProperties
                                            where p.Property.Name.ToLower() == "stock"
                                            select p;

                    if (isClosedConflict.Any())
                    {
                        var serverValue = ec.Details.EntityConflict.ConflictingProperties.First().ServerValue;
                        var clientValue = ec.Details.EntityConflict.ConflictingProperties.First().Property.Value;
                        if ((Boolean)serverValue == false && (Boolean)clientValue == true)
                        {
                            ec.Details.EntityConflict.ResolveConflicts(ConflictResolution.ClientWins);
                            save = true;
                        }
                        else
                        {
                            ec.Details.DiscardChanges();
                            if ((Boolean)clientValue == true)
                            {
                                this.ShowMessageBox("The purchase order created at " + ((PurchaseOrder)ec).CreatedTime +
                                    " has been closed already");
                            }
                        }
                    }
                    else if (drugStockConflict.Any())
                    {
                        var clientValue = ec.Details.EntityConflict.ConflictingProperties.First().Property.Value;
                        var origianlValue = ec.Details.EntityConflict.ConflictingProperties.First().OriginalValue;                      

                        Drug drug = this.DataWorkspace.ApplicationData.Drugs_SingleOrDefault(((Drug)ec).Id);
                        if (drug != null)
                        {
                            drug.Stock += (int)clientValue - (int)origianlValue;
                            save = true;
                        }
                        ec.Details.EntityConflict.ResolveConflicts(ConflictResolution.ClientWins);
                    }
                    else
                        ec.Details.EntityConflict.ResolveConflicts(ConflictResolution.ServerWins);
                }

                if (save)
                {
                    StockManagement_Saving(ref handled);
                }
            }
        }
    }
}
